import java.io.*;
import java.util.*;

public class StudentManager {
    private static StudentManager studentManager = null;
    private List<Student> studentList;

    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }

    private StudentManager() {
    }

    public static StudentManager getInstance() {
        if (studentManager == null) {
            studentManager = new StudentManager();
        }
        return studentManager;
    }

    public void printAll() {
        System.out.println("All the students' information is below");
        printStudentList(studentList);
    }

    public void getStudentById(String id) {
        for (Student student : studentList) {
            if (student.studentID.equals(id)) {
                System.out.printf("The student information of studentID %s is as follows\n", id);
                int totalScore = student.chineseScore + student.mathScore + student.englishScore;
                double averageScore = totalScore / 3.0;
                System.out.printf("%s,%s,%d,%d,%d,%d,%.1f\n", student.studentID, student.name, student.chineseScore, student.mathScore, student.englishScore, totalScore, averageScore);
            }
        }
    }

    public void findByScore(String classType, int min, int max) {
        List<Student> classifiedList = new ArrayList<>();
        switch (classType) {
            case "语文":
                for (Student student : studentList) {
                    if (student.chineseScore >= min && student.chineseScore <= max) classifiedList.add(student);
                }
                break;
            case "数学":
                for (Student student : studentList) {
                    if (student.mathScore >= min && student.mathScore <= max) classifiedList.add(student);
                }
                break;
            case "外语":
                for (Student student : studentList) {
                    if (student.englishScore >= min && student.englishScore <= max) classifiedList.add(student);
                }
                break;
            default:
                System.out.println("No such subject!");
        }
        System.out.printf("Students who scored between %d and %d in %s subjects are as follows:\n", min, max, classType);
        printStudentList(classifiedList);
        classifiedList.clear();
    }

    public void findByScore(int min, int max) {
        List<Student> classifiedList = new ArrayList<>();
        for (Student student : studentList) {
            double averageScore = (student.chineseScore + student.mathScore + student.englishScore) / 3.0;
            if (averageScore >= min && averageScore <= max) {
                classifiedList.add(student);
            }
        }
        System.out.printf("Students who scored between %d and %d in average score are as follows:\n", min, max);
        printStudentList(classifiedList);
    }

    public void sortByTotal() {
        List<Student> sortedList = new ArrayList<>(studentList);
        for (int i = 0; i < sortedList.size() - 1; i++) {
            for (int j = i; j < sortedList.size(); j++) {
//                懒得用其他的排序了，就冒泡吧
                if (sortedList.get(i).hashCode() < sortedList.get(j).hashCode()) {
                    Student temp = sortedList.get(i);
                    sortedList.set(i, sortedList.get(j));
                    sortedList.set(j, temp);
                }
            }
        }
        System.out.println("After sorting by total score:");
        printStudentList(sortedList);
    }

    public void saveAs(String filePath) {
        try {
            File savedFile = new File(filePath);
            if (!savedFile.exists()) {
                savedFile.createNewFile();
            }
            FileWriter fileWriter = new FileWriter(savedFile);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            for (Student student : studentList) {
                bufferedWriter.write(student.formatString());
            }
            bufferedWriter.flush();
            bufferedWriter.close();
            System.out.printf("File saved in path:%s successfully!\n", filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void printStudentList(List<Student> myStudentList) {
        for (Student student : myStudentList) {
            int totalScore = student.chineseScore + student.mathScore + student.englishScore;
            double averageScore = totalScore / 3.0;
            System.out.printf("%s,%s,%d,%d,%d,%d,%.1f\n", student.studentID, student.name, student.chineseScore, student.mathScore, student.englishScore, totalScore, averageScore);
        }
        System.out.println();
    }

}
